import toolMgr from '@/biz/base/tool.js'
import ruleOpt from '@/biz/option/rule.js'
import dt from '@dt/dt'
let _this = null

function options(vm) {
  _this = vm
  let opt = {
    title: '标题底色',
    key: 'titleBackColor',
    visible: _this.info.templateType == 3 || _this.info.templateType == 4,
    placeholder: '请输入色值',
    input: {
      type: 'text',
      prependTxt: '#'
    },
    rules: [
      {
        required: true,
        message: '请填写色值',
        trigger: 'blur'
      },
      ruleOpt.lettersNumber()
    ]
  }
  return [
    {
      title: '模板名',
      key: 'templateName',
      placeholder: '请输入模板名称',
      input: {
        type: 'text'
      },
      rules: [
        {
          required: true,
          message: '请输入模板名称',
          trigger: 'blur'
        }
      ]
    },
    {
      key: 'templateType',
      title: '模板类型',
      placeholder: '请选择模板类型',
      minWidth: 220,
      select: {
        options: dt.dictInfo().templateType
      },
      change: (data) => {
        console.log(_this.info.templateType, '选中模板')
        if (_this.info.templateType == 3 || _this.info.templateType == 4) {
          opt.visible = true
        } else {
          opt.visible = false
        }
      },

      rules: [
        {
          required: true,
          message: '请选择素材模板',
          trigger: 'blur'
        }
      ]
    },
    accessoryImg1(_this.info),
    accessoryImg2(_this.info),
    accessoryImg3(_this.info),
    {
      title: '标题字色',
      key: 'titleFontColor',
      placeholder: '请输入色值',
      input: {
        type: 'text',
        prependTxt: '#'
      },
      rules: [
        {
          required: true,
          message: '请填写色值',
          trigger: 'blur'
        },
        ruleOpt.lettersNumber()
      ]
    },
    opt,
    {
      title: '图标色值',
      key: 'iconColor',
      placeholder: '请输入色值',
      input: {
        type: 'text',
        prependTxt: '#'
      },
      rules: [
        {
          required: true,
          message: '请填写色值',
          trigger: 'blur'
        },
        ruleOpt.lettersNumber()
      ]
    },
    {
      title: '中部色值',
      key: 'middleBackColor',
      placeholder: '请输入色值',
      input: {
        type: 'text',
        prependTxt: '#'
      },
      rules: [
        {
          required: true,
          message: '请填写色值',
          trigger: 'blur'
        },
        ruleOpt.lettersNumber()
      ]
    },
    {
      title: '头尾色值',
      key: 'headTailFontColor',
      placeholder: '请输入色值',
      input: {
        type: 'text',
        prependTxt: '#'
      },
      rules: [
        {
          required: true,
          message: '请填写色值',
          trigger: 'blur'
        },
        ruleOpt.lettersNumber()
      ]
    }
  ]
}

export default {
  options
}

function accessoryImg1(info) {
  console.log(info, '11vv')
  let api = toolMgr.unloadFile()
  let files = []
  if (info.accessoryBOList1 && info.accessoryBOList1.length > 0) {
    info.accessoryBOList1.forEach((item) => {
      if (item) {
        files.push({
          name: item.accessoryName,
          url: item.accessoryUrl
        })
      }
    })
  }

  let opt = {
    title: '上传',
    api,
    files,
    multiple: true,
    show: false,
    maxlength: 1,
    hasCover: false,
    format: ['jpg', 'jpeg', 'png'],
    formatErr: () => {
      _this.$Message.error('只支持上传jpg、jpeg、png图片！')
    },
    preview: (file) => {
      toolMgr.downloadFile(file.url, file.name)
    }
  }
  return {
    title: '头部图片',
    key: 'accessoryBOList1',
    uploadImg: opt,
    data: {
      to: (v) => {
        let accessoryBOList1 = v.map((item) => {
          console.log(v, item, 'ddddd')
          return {
            name: item.name,
            url: item.response ? item.response.data.imageURL : item.url,
            accessoryName: item.name,
            accessoryUrl: item.response ? item.response.data.imageURL : item.url
          }
        })
        return {
          accessoryBOList1
        }
      }
    },
    rules: [
      {
        required: true,
        message: '请上传',
        type: 'array',
        trigger: 'blur'
      }
    ]
  }
}

function accessoryImg2(info) {
  let api = toolMgr.unloadFile()
  let files = []
  if (info.accessoryBOList2 && info.accessoryBOList2.length > 0) {
    info.accessoryBOList2.forEach((item) => {
      if (item) {
        files.push({
          name: item.accessoryName,
          url: item.accessoryUrl
        })
      }
    })
  }

  let opt = {
    title: '上传',
    api,
    files,
    multiple: true,
    show: false,
    maxlength: 1,
    hasCover: false,
    format: ['jpg', 'jpeg', 'png'],
    formatErr: () => {
      _this.$Message.error('只支持上传jpg、jpeg、png图片！')
    },
    preview: (file) => {
      toolMgr.downloadFile(file.url, file.name)
    }
  }
  return {
    title: '尾部图片',
    key: 'accessoryBOList2',
    uploadImg: opt,
    data: {
      to: (v) => {
        let accessoryBOList2 = v.map((item) => {
          return {
            name: item.name,
            url: item.response ? item.response.data.imageURL : item.url,
            accessoryName: item.name,
            accessoryUrl: item.response ? item.response.data.imageURL : item.url
          }
        })
        return {
          accessoryBOList2
        }
      }
    },
    rules: [
      {
        required: true,
        message: '请上传',
        type: 'array',
        trigger: 'blur'
      }
    ]
  }
}

function accessoryImg3(info) {
  let api = toolMgr.unloadFile()
  let files = []
  if (info.accessoryBOList3 && info.accessoryBOList3.length > 0) {
    info.accessoryBOList3.forEach((item) => {
      if (item) {
        files.push({
          name: item.accessoryName,
          url: item.accessoryUrl
        })
      }
    })
  }

  let opt = {
    title: '上传',
    api,
    files,
    multiple: true,
    show: false,
    maxlength: 1,
    hasCover: false,
    format: ['jpg', 'jpeg', 'png'],
    formatErr: () => {
      _this.$Message.error('只支持上传jpg、jpeg、png图片！')
    },
    preview: (file) => {
      toolMgr.downloadFile(file.url, file.name)
    }
  }
  return {
    title: '缩略图',
    key: 'accessoryBOList3',
    uploadImg: opt,
    data: {
      to: (v) => {
        let accessoryBOList3 = v.map((item) => {
          return {
            name: item.name,
            url: item.response ? item.response.data.imageURL : item.url,
            accessoryName: item.name,
            accessoryUrl: item.response ? item.response.data.imageURL : item.url
          }
        })
        return {
          accessoryBOList3
        }
      }
    },
    rules: [
      {
        required: true,
        message: '请上传',
        type: 'array',
        trigger: 'blur'
      }
    ]
  }
}
